package com.wyx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wyx.entity.User;
import com.wyx.service.UserService;
import com.wyx.util.JwtUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wyx
 * @since 2022-03-15
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService us;
    @GetMapping("/{name}/{pwd}")
    public Map<String, Object> select(@PathVariable("name") String name, @PathVariable("pwd") String pwd){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",name).eq("password",pwd);
        User user = us.getOne(wrapper);
        Map<String, Object> hashMap = new HashMap<>();
        if (user!=null){
           Map<String, String> map = new HashMap();
           map.put("username", user.getUserName());
            String token = JwtUtils.getToken(map);
            hashMap.put("token",token);
            hashMap.put("username", user.getUserName());
            hashMap.put("status","sccess");
        }else {
            hashMap.put("msg","失败");
        }
        return hashMap;
    }
    @PostMapping
    public Map<String, Object> register(@RequestBody User user){
        us.save(user);
        return new HashMap<String, Object>(){{
           put("status","success");
        }};
    }
}

